import { roleList, removeUser, addRole, editUser, roleDetail, rolePermission, roleBindPower } from "../api/role";
import { message } from "antd/lib/index";
import checkStatus from '@/utils/status';

export default {
  namespace: "roleModels",

  state: {
    data: {
      data: [],
      pagination: {}
    }
  },
  effects: {
    //请求列表
    * fetch({ payload }, { call, put }) {
      const response = yield call(roleList, payload);
      yield checkStatus(response)
      yield put({
        type: "get",
        payload: response.msg
      });
    },
    //增加
    * add({ payload, callback }, { call, put }) {
      const response = yield call(addRole, payload);
      yield checkStatus(response)

      yield put({
        type: "save",
        payload: response
      });
      if (callback) callback();
    },

    * remove({ payload, callback }, { call, put }) {
      const response = yield call(removeUser, payload);
      yield checkStatus(response)

      yield put({
        type: "userRemove",
        payload: response
      });
      if (callback) callback();
    },

    * update({ payload, callback }, { call, put }) {
      const response = yield call(editUser, payload);
      yield checkStatus(response)

      yield put({
        type: "edit",
        payload: response
      });
      if (callback) callback();
    },
    * rolePermission({ payload, callback }, { call, put }) {
      const response = yield call(rolePermission, payload);
      yield checkStatus(response)

      yield put({
        type: "getRolePermission",
        payload: response.msg
      });
      if (callback) callback(response.msg);
    },
    * getRoleDesc({ payload, callback }, { call, put }) {
      const response = yield call(roleDetail, payload);
      yield checkStatus(response)

      yield put({
        type: "getdesc",
        payload: response.msg
      });
      if (callback) callback();
    },

    * rolebingpowe({ payload, callback }, { call, put }) {
      const response = yield call(roleBindPower, payload);
      yield checkStatus(response)
      if (callback) callback(response);
    }
  },

  reducers: {
    get(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total
      };
      return {
        ...state,
        data: action.payload
      };
    },
    save(state, action) {
      if (action.payload.code === 200) {
        message.success("添加成功");
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload.msg
      };
    },
    userRemove(state, action) {
      return {
        ...state,
        data: action.payload
      };
    },
    edit(state, action) {
      if (action.payload.code === 200) {
        message.success("更新成功");
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload
      };
    },

    getdesc(state, action) {
      return {
        ...state,
        roleDesc: action.payload
      };
    },
    getRolePermission(state, action) {
      return {
        ...state,
        rolePermission: action.payload
      };
    }
  }
};
